{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "befeff8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pyecharts.options as opts\n",
    "from pyecharts.charts import Pie,Bar\n",
    "from pyecharts.commons.utils import JsCode\n",
    "from pyecharts.globals import ThemeType"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cd97979b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>survived</th>\n",
       "      <th>pclass</th>\n",
       "      <th>sex</th>\n",
       "      <th>age</th>\n",
       "      <th>sibsp</th>\n",
       "      <th>parch</th>\n",
       "      <th>fare</th>\n",
       "      <th>embarked</th>\n",
       "      <th>class</th>\n",
       "      <th>who</th>\n",
       "      <th>adult_male</th>\n",
       "      <th>deck</th>\n",
       "      <th>embark_town</th>\n",
       "      <th>alive</th>\n",
       "      <th>alone</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>S</td>\n",
       "      <td>Third</td>\n",
       "      <td>man</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>no</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C</td>\n",
       "      <td>First</td>\n",
       "      <td>woman</td>\n",
       "      <td>False</td>\n",
       "      <td>C</td>\n",
       "      <td>Cherbourg</td>\n",
       "      <td>yes</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>S</td>\n",
       "      <td>Third</td>\n",
       "      <td>woman</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>yes</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>S</td>\n",
       "      <td>First</td>\n",
       "      <td>woman</td>\n",
       "      <td>False</td>\n",
       "      <td>C</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>yes</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>S</td>\n",
       "      <td>Third</td>\n",
       "      <td>man</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>no</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>886</th>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>male</td>\n",
       "      <td>27.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>13.0000</td>\n",
       "      <td>S</td>\n",
       "      <td>Second</td>\n",
       "      <td>man</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>no</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>887</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>female</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30.0000</td>\n",
       "      <td>S</td>\n",
       "      <td>First</td>\n",
       "      <td>woman</td>\n",
       "      <td>False</td>\n",
       "      <td>B</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>yes</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>888</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>female</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>23.4500</td>\n",
       "      <td>S</td>\n",
       "      <td>Third</td>\n",
       "      <td>woman</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>no</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>889</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>male</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30.0000</td>\n",
       "      <td>C</td>\n",
       "      <td>First</td>\n",
       "      <td>man</td>\n",
       "      <td>True</td>\n",
       "      <td>C</td>\n",
       "      <td>Cherbourg</td>\n",
       "      <td>yes</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>890</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>male</td>\n",
       "      <td>32.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.7500</td>\n",
       "      <td>Q</td>\n",
       "      <td>Third</td>\n",
       "      <td>man</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Queenstown</td>\n",
       "      <td>no</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>891 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     survived  pclass     sex   age  sibsp  parch     fare embarked   class  \\\n",
       "0           0       3    male  22.0      1      0   7.2500        S   Third   \n",
       "1           1       1  female  38.0      1      0  71.2833        C   First   \n",
       "2           1       3  female  26.0      0      0   7.9250        S   Third   \n",
       "3           1       1  female  35.0      1      0  53.1000        S   First   \n",
       "4           0       3    male  35.0      0      0   8.0500        S   Third   \n",
       "..        ...     ...     ...   ...    ...    ...      ...      ...     ...   \n",
       "886         0       2    male  27.0      0      0  13.0000        S  Second   \n",
       "887         1       1  female  19.0      0      0  30.0000        S   First   \n",
       "888         0       3  female   NaN      1      2  23.4500        S   Third   \n",
       "889         1       1    male  26.0      0      0  30.0000        C   First   \n",
       "890         0       3    male  32.0      0      0   7.7500        Q   Third   \n",
       "\n",
       "       who  adult_male deck  embark_town alive  alone  \n",
       "0      man        True  NaN  Southampton    no  False  \n",
       "1    woman       False    C    Cherbourg   yes  False  \n",
       "2    woman       False  NaN  Southampton   yes   True  \n",
       "3    woman       False    C  Southampton   yes  False  \n",
       "4      man        True  NaN  Southampton    no   True  \n",
       "..     ...         ...  ...          ...   ...    ...  \n",
       "886    man        True  NaN  Southampton    no   True  \n",
       "887  woman       False    B  Southampton   yes   True  \n",
       "888  woman       False  NaN  Southampton    no  False  \n",
       "889    man        True    C    Cherbourg   yes   True  \n",
       "890    man        True  NaN   Queenstown    no   True  \n",
       "\n",
       "[891 rows x 15 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('titanic.csv')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0b833f49",
   "metadata": {},
   "outputs": [],
   "source": [
    "alone_ct = data.groupby(['alone','alive'])['who'].count()\n",
    "f = alone_ct[False]\n",
    "t = alone_ct[True]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b20fb71e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def convert(x):\n",
    "    if x=='no':\n",
    "        return '牺牲'\n",
    "    else:\n",
    "        return '幸存'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4d313b64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/v5/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"f01cac0a96024cd38c1113d8d5e5c9fc\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_f01cac0a96024cd38c1113d8d5e5c9fc = echarts.init(\n",
       "                    document.getElementById('f01cac0a96024cd38c1113d8d5e5c9fc'), 'white', {renderer: 'canvas'});\n",
       "                var option_f01cac0a96024cd38c1113d8d5e5c9fc = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"aria\": {\n",
       "        \"enabled\": false\n",
       "    },\n",
       "    \"color\": [\n",
       "        \"#5470c6\",\n",
       "        \"#91cc75\",\n",
       "        \"#fac858\",\n",
       "        \"#ee6666\",\n",
       "        \"#73c0de\",\n",
       "        \"#3ba272\",\n",
       "        \"#fc8452\",\n",
       "        \"#9a60b4\",\n",
       "        \"#ea7ccc\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"name\": \"\\u975e\\u72ec\\u81ea\\u767b\\u8239\",\n",
       "            \"colorBy\": \"data\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"selectedMode\": false,\n",
       "            \"selectedOffset\": 10,\n",
       "            \"clockwise\": true,\n",
       "            \"startAngle\": 90,\n",
       "            \"minAngle\": 0,\n",
       "            \"minShowLabelAngle\": 0,\n",
       "            \"avoidLabelOverlap\": true,\n",
       "            \"stillShowZeroSum\": true,\n",
       "            \"percentPrecision\": 2,\n",
       "            \"showEmptyCircle\": true,\n",
       "            \"emptyCircleStyle\": {\n",
       "                \"color\": \"lightgray\",\n",
       "                \"borderColor\": \"#000\",\n",
       "                \"borderWidth\": 0,\n",
       "                \"borderType\": \"solid\",\n",
       "                \"borderDashOffset\": 0,\n",
       "                \"borderCap\": \"butt\",\n",
       "                \"borderJoin\": \"bevel\",\n",
       "                \"borderMiterLimit\": 10,\n",
       "                \"opacity\": 1\n",
       "            },\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"no\",\n",
       "                    \"value\": 175\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"yes\",\n",
       "                    \"value\": 179\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"25%\",\n",
       "                \"40%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"30%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"outside\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{a|{a}}\\n{hr|}\\n {b|{b}: }{c} {per|{d}%}  \",\n",
       "                \"borderColor\": \"#aaa\",\n",
       "                \"borderWidth\": 1,\n",
       "                \"borderRadius\": 4,\n",
       "                \"rich\": {\n",
       "                    \"a\": {\n",
       "                        \"color\": \"#999\",\n",
       "                        \"lineHeight\": 22,\n",
       "                        \"align\": \"center\"\n",
       "                    },\n",
       "                    \"hr\": {\n",
       "                        \"borderColor\": \"#aaa\",\n",
       "                        \"width\": \"100%\",\n",
       "                        \"borderWidth\": 0.5,\n",
       "                        \"height\": 0\n",
       "                    },\n",
       "                    \"b\": {\n",
       "                        \"fontSize\": 16,\n",
       "                        \"lineHeight\": 33\n",
       "                    },\n",
       "                    \"per\": {\n",
       "                        \"color\": \"#eee\",\n",
       "                        \"backgroundColor\": \"#334455\",\n",
       "                        \"padding\": [\n",
       "                            2,\n",
       "                            4\n",
       "                        ],\n",
       "                        \"borderRadius\": 2\n",
       "                    }\n",
       "                }\n",
       "            },\n",
       "            \"labelLine\": {\n",
       "                \"show\": true,\n",
       "                \"showAbove\": false,\n",
       "                \"length\": 15,\n",
       "                \"length2\": 15,\n",
       "                \"smooth\": false,\n",
       "                \"minTurnAngle\": 90,\n",
       "                \"maxSurfaceAngle\": 90\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"name\": \"\\u72ec\\u81ea\\u767b\\u8239\",\n",
       "            \"colorBy\": \"data\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"selectedMode\": false,\n",
       "            \"selectedOffset\": 10,\n",
       "            \"clockwise\": true,\n",
       "            \"startAngle\": 90,\n",
       "            \"minAngle\": 0,\n",
       "            \"minShowLabelAngle\": 0,\n",
       "            \"avoidLabelOverlap\": true,\n",
       "            \"stillShowZeroSum\": true,\n",
       "            \"percentPrecision\": 2,\n",
       "            \"showEmptyCircle\": true,\n",
       "            \"emptyCircleStyle\": {\n",
       "                \"color\": \"lightgray\",\n",
       "                \"borderColor\": \"#000\",\n",
       "                \"borderWidth\": 0,\n",
       "                \"borderType\": \"solid\",\n",
       "                \"borderDashOffset\": 0,\n",
       "                \"borderCap\": \"butt\",\n",
       "                \"borderJoin\": \"bevel\",\n",
       "                \"borderMiterLimit\": 10,\n",
       "                \"opacity\": 1\n",
       "            },\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"no\",\n",
       "                    \"value\": 374\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"yes\",\n",
       "                    \"value\": 163\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"25%\",\n",
       "                \"40%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"70%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"outside\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{a|{a}}\\n{hr|}\\n {b|{b}: }{c} {per|{d}%}  \",\n",
       "                \"borderColor\": \"#aaa\",\n",
       "                \"borderWidth\": 1,\n",
       "                \"borderRadius\": 4,\n",
       "                \"rich\": {\n",
       "                    \"a\": {\n",
       "                        \"color\": \"#999\",\n",
       "                        \"lineHeight\": 22,\n",
       "                        \"align\": \"center\"\n",
       "                    },\n",
       "                    \"hr\": {\n",
       "                        \"borderColor\": \"#aaa\",\n",
       "                        \"width\": \"100%\",\n",
       "                        \"borderWidth\": 0.5,\n",
       "                        \"height\": 0\n",
       "                    },\n",
       "                    \"b\": {\n",
       "                        \"fontSize\": 16,\n",
       "                        \"lineHeight\": 33\n",
       "                    },\n",
       "                    \"per\": {\n",
       "                        \"color\": \"#eee\",\n",
       "                        \"backgroundColor\": \"#334455\",\n",
       "                        \"padding\": [\n",
       "                            2,\n",
       "                            4\n",
       "                        ],\n",
       "                        \"borderRadius\": 2\n",
       "                    }\n",
       "                }\n",
       "            },\n",
       "            \"labelLine\": {\n",
       "                \"show\": true,\n",
       "                \"showAbove\": false,\n",
       "                \"length\": 15,\n",
       "                \"length2\": 15,\n",
       "                \"smooth\": false,\n",
       "                \"minTurnAngle\": 90,\n",
       "                \"maxSurfaceAngle\": 90\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"no\",\n",
       "                \"yes\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14,\n",
       "            \"backgroundColor\": \"transparent\",\n",
       "            \"borderColor\": \"#ccc\",\n",
       "            \"borderRadius\": 0,\n",
       "            \"pageButtonItemGap\": 5,\n",
       "            \"pageButtonPosition\": \"end\",\n",
       "            \"pageFormatter\": \"{current}/{total}\",\n",
       "            \"pageIconColor\": \"#2f4554\",\n",
       "            \"pageIconInactiveColor\": \"#aaa\",\n",
       "            \"pageIconSize\": 15,\n",
       "            \"animationDurationUpdate\": 800,\n",
       "            \"selector\": false,\n",
       "            \"selectorPosition\": \"auto\",\n",
       "            \"selectorItemGap\": 7,\n",
       "            \"selectorButtonGap\": 10\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"enterable\": false,\n",
       "        \"confine\": false,\n",
       "        \"appendToBody\": false,\n",
       "        \"transitionDuration\": 0.4,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5,\n",
       "        \"order\": \"seriesAsc\"\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"text\": \"\\u5e78\\u5b58\\u4e0e\\u5426\\u4e0e\\u72ec\\u7acb\\u767b\\u8239\\u5173\\u7cfb\\u5206\\u6790\",\n",
       "            \"target\": \"blank\",\n",
       "            \"subtarget\": \"blank\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"textAlign\": \"auto\",\n",
       "            \"textVerticalAlign\": \"auto\",\n",
       "            \"triggerEvent\": false\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_f01cac0a96024cd38c1113d8d5e5c9fc.setOption(option_f01cac0a96024cd38c1113d8d5e5c9fc);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1eaba8ab950>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi = (\n",
    "    Pie()\n",
    "    .add(\n",
    "        \"非独自登船\",\n",
    "        [[convert(z[0]),int(z[1])] for z in zip(f.index, f.values)],\n",
    "        radius=[\"25%\", \"40%\"],\n",
    "        center=[\"30%\", \"50%\"],\n",
    "        label_opts=opts.LabelOpts(\n",
    "            position=\"outside\",\n",
    "            formatter=\"{a|{a}}\\n{hr|}\\n {b|{b}: }{c} {per|{d}%}  \",\n",
    "            border_color=\"#aaa\",\n",
    "            border_width=1,\n",
    "            border_radius=4,\n",
    "            rich={\n",
    "                \"a\": {\"color\": \"#999\", \"lineHeight\": 22, \"align\": \"center\"},\n",
    "                \"hr\": {\n",
    "                    \"borderColor\": \"#aaa\",\n",
    "                    \"width\": \"100%\",\n",
    "                    \"borderWidth\": 0.5,\n",
    "                    \"height\": 0,\n",
    "                },\n",
    "                \"b\": {\"fontSize\": 16, \"lineHeight\": 33},\n",
    "                \"per\": {\n",
    "                    \"color\": \"#eee\",\n",
    "                    \"backgroundColor\": \"#334455\",\n",
    "                    \"padding\": [2, 4],\n",
    "                    \"borderRadius\": 2,\n",
    "                },\n",
    "            },\n",
    "        ),\n",
    "    )\n",
    "    .add(\n",
    "        \"独自登船\",\n",
    "        [[convert(z[0]),int(z[1])] for z in zip(t.index, t.values)],\n",
    "        radius=[\"25%\", \"40%\"],\n",
    "        center=[\"70%\", \"50%\"],\n",
    "        label_opts=opts.LabelOpts(\n",
    "            position=\"outside\",\n",
    "            formatter=\"{a|{a}}\\n{hr|}\\n {b|{b}: }{c} {per|{d}%}  \",\n",
    "            border_color=\"#aaa\",\n",
    "            border_width=1,\n",
    "            border_radius=4,\n",
    "            rich={\n",
    "                \"a\": {\"color\": \"#999\", \"lineHeight\": 22, \"align\": \"center\"},\n",
    "                \"hr\": {\n",
    "                    \"borderColor\": \"#aaa\",\n",
    "                    \"width\": \"100%\",\n",
    "                    \"borderWidth\": 0.5,\n",
    "                    \"height\": 0,\n",
    "                },\n",
    "                \"b\": {\"fontSize\": 16, \"lineHeight\": 33},\n",
    "                \"per\": {\n",
    "                    \"color\": \"#eee\",\n",
    "                    \"backgroundColor\": \"#334455\",\n",
    "                    \"padding\": [2, 4],\n",
    "                    \"borderRadius\": 2,\n",
    "                },\n",
    "            },\n",
    "        ),\n",
    "    )\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"幸存与否与独立登船关系分析\"))\n",
    ")\n",
    "pi.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ae2feaf9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data['age_g'] = pd.cut(data['age'], [-1,1,5,13,34,59,200],labels=['婴幼儿','儿童','少年','青年','中年','老年'])\n",
    "age_g_alive = data.groupby(['alive','age_g'])['who'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ca63ffe0",
   "metadata": {},
   "outputs": [],
   "source": [
    "no = age_g_alive['no']\n",
    "no2 = [[z[0],int(z[1])] for z in zip(no.index, no.values)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7f5aebac",
   "metadata": {},
   "outputs": [],
   "source": [
    "yes  = age_g_alive['yes']\n",
    "yes2 = [[z[0],int(z[1])] for z in zip(yes.index, yes.values)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "250f5889",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "no_data = [{'value':x[1],'percent':x[1]/(x[1]+y[1])} for x,y in zip(no2,yes2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "823833a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "yes_data = [{'value':y[1],'percent':y[1]/(y[1]+x[1])} for x,y in zip(no2,yes2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "48c1a8f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/v5/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"4add2ec8d9b64d3480842c2df5b9e73a\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_4add2ec8d9b64d3480842c2df5b9e73a = echarts.init(\n",
       "                    document.getElementById('4add2ec8d9b64d3480842c2df5b9e73a'), 'light', {renderer: 'canvas'});\n",
       "                var option_4add2ec8d9b64d3480842c2df5b9e73a = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"aria\": {\n",
       "        \"enabled\": false\n",
       "    },\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u727a\\u7272\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"value\": 2,\n",
       "                    \"percent\": 0.14285714285714285\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 11,\n",
       "                    \"percent\": 0.36666666666666664\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 16,\n",
       "                    \"percent\": 0.5925925925925926\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 253,\n",
       "                    \"percent\": 0.6216216216216216\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 123,\n",
       "                    \"percent\": 0.5857142857142857\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 19,\n",
       "                    \"percent\": 0.7307692307692307\n",
       "                }\n",
       "            ],\n",
       "            \"realtimeSort\": false,\n",
       "            \"showBackground\": false,\n",
       "            \"stackStrategy\": \"samesign\",\n",
       "            \"cursor\": \"pointer\",\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"50%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": function(x){return Number(x.data.percent * 100).toFixed() + '%';}\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u5e78\\u5b58\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"value\": 12,\n",
       "                    \"percent\": 0.8571428571428571\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 19,\n",
       "                    \"percent\": 0.6333333333333333\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 11,\n",
       "                    \"percent\": 0.4074074074074074\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 154,\n",
       "                    \"percent\": 0.3783783783783784\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 87,\n",
       "                    \"percent\": 0.4142857142857143\n",
       "                },\n",
       "                {\n",
       "                    \"value\": 7,\n",
       "                    \"percent\": 0.2692307692307692\n",
       "                }\n",
       "            ],\n",
       "            \"realtimeSort\": false,\n",
       "            \"showBackground\": false,\n",
       "            \"stackStrategy\": \"samesign\",\n",
       "            \"cursor\": \"pointer\",\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"50%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": function(x){return Number(x.data.percent * 100).toFixed() + '%';}\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u727a\\u7272\",\n",
       "                \"\\u5e78\\u5b58\"\n",
       "            ],\n",
       "            \"selected\": {}\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"enterable\": false,\n",
       "        \"confine\": false,\n",
       "        \"appendToBody\": false,\n",
       "        \"transitionDuration\": 0.4,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5,\n",
       "        \"order\": \"seriesAsc\"\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": true,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"\\u5a74\\u5e7c\\u513f\",\n",
       "                \"\\u513f\\u7ae5\",\n",
       "                \"\\u5c11\\u5e74\",\n",
       "                \"\\u9752\\u5e74\",\n",
       "                \"\\u4e2d\\u5e74\",\n",
       "                \"\\u8001\\u5e74\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": true,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_4add2ec8d9b64d3480842c2df5b9e73a.setOption(option_4add2ec8d9b64d3480842c2df5b9e73a);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1eaba8e0b90>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))\n",
    "    .add_xaxis(list(no.index))\n",
    "    .add_yaxis(\"牺牲\", no_data, category_gap=\"50%\")\n",
    "    .add_yaxis(\"幸存\", yes_data, category_gap=\"50%\")\n",
    "    .set_series_opts(\n",
    "        label_opts=opts.LabelOpts(\n",
    "            position=\"top\",\n",
    "            formatter=JsCode(\n",
    "                \"function(x){return Number(x.data.percent * 100).toFixed() + '%';}\"\n",
    "            )\n",
    "        )\n",
    "    )\n",
    ")\n",
    "bar.render_notebook()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
